package io.github.jast90.internship.controller;

import io.github.jast90.internship.service.OssService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * Created by jast90 on 2021/1/1
 */
@RestController
public class OssController {

    @Autowired
    private OssService ossService;

    @PreAuthorize("hasAuthority('oss:create')")
    @PostMapping("oss/upload/{bucketName}")
    public String upload(@RequestParam("file") MultipartFile file,
                       @PathVariable("bucketName")String bucketName) throws IOException {
        String url = ossService.uploadFile(file.getInputStream(),bucketName,file.getOriginalFilename());
        return url;
    }

    @PreAuthorize("hasAuthority('oss:create')")
    @PostMapping("oss/upload")
    public String upload1(@RequestParam("file") MultipartFile file) throws IOException {
        String url = ossService.uploadFile(file.getInputStream(),"jastz",file.getOriginalFilename());
        return url;
    }
}
